<template>
	<div class="sw_section_container" v-loading="loading">
		<div class="sw_query_form" v-if="$isHasAuth(topFrames)">
			<template v-if="dataType === 'schedule'">
				<div class="button" v-if="$isHasAuth(topButton)">
					<el-button type="primary" icon="el-icon-plus" v-hasPermi="'9d62dcc8502445859c97325284791298'" @click="handleOpenDialog()">
						安排新实验
					</el-button>
					<el-button type="danger" icon="el-icon-delete" v-hasPermi="'7635f90955f84bb483e399c2343a122c'" @click="handleBacthDelete()">删除</el-button>
				</div>
			</template>
			<el-form size="small" inline v-hasPermi="queryBtn">
				<el-form-item>
					<el-input v-model="queryData.filed7" clearable placeholder="实验名称" style="width: 180px"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="el-icon-search" @click="queryTableData">查询</el-button>
				</el-form-item>
			</el-form>
		</div>
		<div class="sw_table_container">
			<el-table size="medium" ref="table" :data="tableData" height="100%" class="index_table_wrapper" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="44" align="center"></el-table-column>
				<el-table-column label="状态" width="80" align="center" :formatter="statusFormatter"></el-table-column>
				<el-table-column label="开始时间" prop="startTime" width="110" sortable></el-table-column>
				<el-table-column label="用户单位" prop="source" width="132" sortable></el-table-column>
				<el-table-column label="申请人" prop="userName" width="92" sortable></el-table-column>
				<el-table-column label="PI" prop="headName" width="84" sortable></el-table-column>
				<el-table-column label="实验名称" prop="filed7" min-width="240" sortable></el-table-column>
				<el-table-column label="测量技术" prop="expName" min-width="100"></el-table-column>
				<el-table-column label="房间" prop="room" min-width="84"></el-table-column>
				<el-table-column label="运维工程师" prop="filed9" min-width="100" sortable></el-table-column>
				<el-table-column label="操作" :fixed="isFixed" :width="$controlWidth(tableButton)" v-if="$isHasAuth(tableButton)">
					<template slot-scope="{ row }">
						<el-button type="text" v-hasPermi="'c252e5cf509f4a6a970ae94d067be86e'" @click="handleLookData(row.id)">详情</el-button>
						<template v-if="userRole === 'manager'">
							<el-button type="text" v-hasPermi="'3148e8291d774e79be88788e9efe7daf'" @click="handleOpenDialog(row.id)">编辑</el-button>
							<template v-if="row.type === '1'">
								<el-button type="text" v-hasPermi="'7635f90955f84bb483e399c2343a122c'" @click="handleDeleteData(row.id)">删除</el-button>
							</template>
							<template v-if="row.type === '2'">
								<el-button type="text" v-hasPermi="'17da844c47ed4b3c95c0f930ff7de222'" @click="handleReturnSummary(row.id)">退回</el-button>
							</template>
						</template>
						<template v-else-if="dataType === 'schedule'">
							<template v-if="row.isSave === '2' || row.audit === '2'">
								<el-button type="text" v-hasPermi="'3148e8291d774e79be88788e9efe7daf'" @click="handleOpenDialog(row.id)">编辑</el-button>
								<el-button type="text" v-hasPermi="'7635f90955f84bb483e399c2343a122c'" @click="handleDeleteData(row.id)">删除</el-button>
							</template>
							<template v-else-if="row.audit === '1'">
								<el-button type="text" v-hasPermi="'52c1ee81464449e3853eca8dc8e038ce'" @click="handleAddesSummary(row)">提交小结</el-button>
							</template>
						</template>
						<template v-else-if="dataType === 'scheduleAudit'">
							<template v-if="row.audit === '3'">
								<el-button type="text" v-hasPermi="'c6a9e3c5fdf848d08535f557f87691e7'" @click="handleAuditData(row.id)">审核</el-button>
							</template>
						</template>
						<template v-else-if="dataType === 'summary'">
							<template v-if="row.type === '2'">
								<el-button type="text" v-hasPermi="'17da844c47ed4b3c95c0f930ff7de222'" @click="handleReturnSummary(row.id)">退回</el-button>
							</template>
						</template>
					</template>
				</el-table-column>
				<el-empty slot="empty" :image-size="120" description="暂无数据"></el-empty>
			</el-table>
		</div>
		<template v-if="dataCount > 0">
			<sw-pagination :total="dataCount" :page.sync="queryData.page" :limit.sync="queryData.limit" @change="queryTableData"></sw-pagination>
		</template>
		<form-dialog v-model="formDialog" :data-id="scheduleId" :user-role="userRole" @finish="queryTableData"></form-dialog>
		<form-summary v-model="summaryDialog" :data="summaryData" @finish="queryTableData"></form-summary>
	</div>
</template>

<script>
import { mapGetters } from 'vuex';
import formDialog from './form';
import formSummary from './summary';
import listMixins from '../listMixins1';
import { queryPageList, queryUserPageList, queryAuditPageList, batchDeleteData, piStatBack } from '@/api/business/tbPiStat';
export default {
	mixins: [listMixins],
	components: { formDialog, formSummary },
	data() {
		return {
			formDialog: false,
			scheduleId: '',
			summaryData: {},
			summaryDialog: false
		};
	},
	computed: {
		...mapGetters(['siteConfig']),
		dataListRequest() {
			if (this.userRole === 'manager') {
				return queryPageList;
			} else if (this.dataType === 'schedule') {
				return queryUserPageList;
			} else if (this.dataType === 'scheduleAudit') {
				return queryAuditPageList;
			} else if (this.dataType === 'summary') {
				return queryUserPageList;
			}
		},
		formType() {
			return this.$route.query.type;
		},
		stationRooms() {
			return this.siteConfig.filter((n) => n.type === '6');
		},
		topButton() {
			if (this.userRole === 'manager') {
				return ['520959e9c7fc4a3b9a1b94759221e08b'];
			} else {
				return ['9d62dcc8502445859c97325284791298', '7635f90955f84bb483e399c2343a122c'];
			}
		},
		topFrames() {
			return [...this.topButton, this.queryBtn];
		},
		tableButton() {
			let detail = 'c252e5cf509f4a6a970ae94d067be86e';
			if (this.userRole === 'manager') {
				if (this.dataType === 'schedule') {
					return [detail, '3148e8291d774e79be88788e9efe7daf', '7635f90955f84bb483e399c2343a122c'];
				} else if (this.dataType === 'summary') {
					return [detail, '3148e8291d774e79be88788e9efe7daf', '17da844c47ed4b3c95c0f930ff7de222'];
				}
			} else if (this.dataType === 'schedule') {
				return [detail, '3148e8291d774e79be88788e9efe7daf', '7635f90955f84bb483e399c2343a122c'];
			} else if (this.dataType === 'scheduleAudit') {
				return [detail, 'c6a9e3c5fdf848d08535f557f87691e7'];
			} else if (this.dataType === 'summary') {
				return [detail, '17da844c47ed4b3c95c0f930ff7de222'];
			}
		}
	},
	watch: {
		formType: {
			async handler(val) {
				if (!val) return false;
				this.$nextTick(() => {
					this.formDialog = true;
				});
			},
			immediate: true
		},
		formDialog(flag) {
			if (!!flag) return false;
			if (!this.formType) return false;
			this.$router.replace(`/${this.userRole}/${this.dataType}/list`);
		}
	},
	methods: {
		//请求参数
		formatQueryParams() {
			let params = { ...this.queryData };
			if (this.userRole === 'manager') {
				params['isSave'] = '1';
				if (this.dataType === 'schedule') {
					params['type'] = '1';
				} else {
					params['type'] = '2';
				}
			} else if (this.dataType === 'schedule') {
				params['type'] = '1';
			} else if (this.dataType === 'scheduleAudit') {
				params['type'] = '1';
			} else if (this.dataType === 'summary') {
				params['type'] = '2';
			}
			return params;
		},

		//获取课题列表数据
		async getTableData() {
			let params = this.formatQueryParams();
			let result = await this.dataListRequest(params).then((res) => res.data);
			this.loading = false;
			if (result.code === '0') {
				this.dataCount = parseInt(result.count);
				this.tableData = result.data || [];
			}
		},

		//删除课题请求
		async deleteDataRequest(id) {
			let params = !id ? this.checkedData.map((n) => n.id) : [id];
			let result = await batchDeleteData(params).then((res) => res.data);
			if (result.code === '0') {
				this.$showCustomMessage('success', '删除数据成功');
				this.queryTableData();
			}
		},

		//打开弹窗
		handleOpenDialog(id) {
			this.scheduleId = !id ? '' : id;
			this.formDialog = true;
		},

		//格式化实验站
		stationFormatter(data) {
			let info = this.stationRooms.find((n) => n.id === data.expName);
			return !info ? '--' : info.name;
		},

		//格式化房间
		roomFormatter(data) {
			let info = this.stationRooms.find((n) => n.id === data.room);
			return !info ? '--' : info.name;
		},

		//提交小结
		handleAddesSummary(data) {
			this.summaryData = { ...data };
			this.summaryDialog = true;
		},

		//退回小结
		handleReturnSummary(id) {
			let that = this;
			that.$confirm('此操作将退回实验提交小结, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await that.returnSummaryRequest(id);
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		async returnSummaryRequest(id) {
			let params = { id };
			let result = await piStatBack(params).then((res) => res.data);
			if (result.code === '0') {
				this.$showCustomMessage('success', `退回实验小结成功`);
				this.queryTableData();
			}
		}
	}
};
</script>
